package com.ykh.mybbsdemo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ykh.mybbsdemo.dao.mapper.ArticleMapper;
import com.ykh.mybbsdemo.dao.mapper.FocusedMapper;
import com.ykh.mybbsdemo.dao.mapper.SysUserMapper;
import com.ykh.mybbsdemo.dao.pojo.po.Focused;
import com.ykh.mybbsdemo.dao.pojo.po.SysUser;
import com.ykh.mybbsdemo.service.FocusedService;
import com.ykh.mybbsdemo.dao.pojo.vo.Result;
import com.ykh.mybbsdemo.dao.pojo.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2023-06-08
 * Time: 13:59
 */
@Service
public class FocusedServiceImpl implements FocusedService {

    @Autowired
    FocusedMapper focusedMapper;
    
    @Autowired
    ArticleMapper articleMapper;
    
    @Autowired
    SysUserMapper sysUserMapper;
    /**
     * 关注用户
     * @param id
     * @param sysUser
     * @return
     */
    @Override
    @Transactional
    public Result focusUser(Long id, SysUser sysUser) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Focused> queryWrapper1 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysUser> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getId, id);
        SysUser sysUser1 = sysUserMapper.selectOne(queryWrapper);
        if(sysUser1 == null){
            return Result.fail(405,"关注的用户不存在");
        }
        //判断是否已关注过该用户
        queryWrapper1.eq(Focused::getLaunchId, sysUser.getId());
        queryWrapper1.eq(Focused::getFocusedId, id);
        queryWrapper1.last("limit 1");
        Focused focused1 = focusedMapper.selectOne(queryWrapper1);
        if(focused1 != null){
            return Result.fail(405,"已关注过该用户");
        }
        Focused focused = new Focused();
        focused.setLaunchId(sysUser.getId());
        focused.setFocusedId(sysUser1.getId());
        focusedMapper.insert(focused);
        queryWrapper2.eq(SysUser::getId, sysUser.getId());
        sysUser1.setFansNum(sysUser1.getFansNum()+1);
        sysUser.setFocusNum(sysUser.getFocusNum() + 1);
        //增加粉丝数,更新关注数
        sysUserMapper.update(sysUser1,queryWrapper);
        sysUserMapper.update(sysUser,queryWrapper2);
        return Result.success("关注成功");
    }

    /**
     * 取消关注用户
     * @param id
     * @param sysUser
     * @return
     */
    @Override
    public Result cancelFocus(Long id, SysUser sysUser) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Focused> queryWrapper1 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysUser> queryWrapper2 = new LambdaQueryWrapper<>();

        queryWrapper.eq(SysUser::getId, id);
        SysUser sysUser1 = sysUserMapper.selectOne(queryWrapper);
        if(sysUser1 == null){
            return Result.fail(405,"关注的用户不存在");
        }
        queryWrapper1.eq(Focused::getLaunchId, sysUser.getId());
        queryWrapper1.eq(Focused::getFocusedId, id);
        Focused focused = focusedMapper.selectOne(queryWrapper1);
        if(focused == null){
            return Result.fail(405,"未关注该用户");
        }
        queryWrapper2.eq(SysUser::getId, sysUser.getId());
        focusedMapper.delete(queryWrapper1);
        sysUser1.setFansNum(sysUser1.getFansNum()-1);
        sysUser.setFocusNum(sysUser.getFocusNum()-1);
        sysUserMapper.update(sysUser1,queryWrapper);
        sysUserMapper.update(sysUser,queryWrapper2);
        return Result.success("取消关注成功");
    }

    /**
     * 获取关注用户的列表
     * @return
     */
    @Override
    public Result getMyFocusList(SysUser sysUser) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Focused> queryWrapper1 = new LambdaQueryWrapper<>();
        //获取关注用户的id列表
        queryWrapper1.eq(Focused::getLaunchId, sysUser.getId());
        List<Focused> focuseds = focusedMapper.selectList(queryWrapper1);
        if(focuseds.size() <= 0){
            return Result.fail(405,"未关注用户");
        }

        List<Long> focusedUserIdList = new ArrayList<>();
        for (Focused focused: focuseds) {
            focusedUserIdList.add(focused.getFocusedId());
        }
        //查询用户表,获取用户信息
        queryWrapper.in(SysUser::getId,focusedUserIdList);
        List<SysUser> focusedUserList = sysUserMapper.selectList(queryWrapper);
        List<UserVo> userVoList = copyList(focusedUserList);
        return Result.success(userVoList);
    }


//    /**
//     * 获取关注用户的列表(视图查询)
//     * @return
//     */
//    @Override
//    public Result getMyFocusList(SysUser sysUser) {
//        List<UserVo> userVoList = focusedMapper.getMyFocusList(sysUser.getId());
//        return Result.success(userVoList);
//    }

    /**
     * 获取关注自己的用户信息列表
     * @return
     */
    @Override
    public Result getFocusedList(SysUser sysUser) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Focused> queryWrapper1 = new LambdaQueryWrapper<>();
        //获取关注用户的id列表
        queryWrapper1.eq(Focused::getFocusedId, sysUser.getId());
        List<Focused> focuseds = focusedMapper.selectList(queryWrapper1);
        if(focuseds.size() <= 0){
            return Result.success("未被用户关注");
        }
        List<Long> launchUserIdList = new ArrayList<>();
        for (Focused focused: focuseds) {
            launchUserIdList.add(focused.getLaunchId());
        }

        //查询用户表,获取用户信息
        queryWrapper.in(SysUser::getId,launchUserIdList);
        List<SysUser> launchUserList = sysUserMapper.selectList(queryWrapper);
        List<UserVo> userVoList = copyList(launchUserList);
        return Result.success(userVoList);
    }

    /**
     * 判断是否关注过该用户
     * @param id
     * @param sysUser
     * @return
     */
    @Override
    public Result isFocus(Long id, SysUser sysUser) {
        //判断是否已关注过该用户
        LambdaQueryWrapper<Focused> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Focused::getLaunchId, sysUser.getId());
        queryWrapper1.eq(Focused::getFocusedId, id);
        queryWrapper1.last("limit 1");
        Focused focused1 = focusedMapper.selectOne(queryWrapper1);
        if(focused1 == null){
            return Result.success("false");
        }
        return Result.success("true");
    }

    private List<UserVo> copyList(List<SysUser> focusedUserList) {
        List<UserVo> userVoList = new ArrayList<>();
        for (SysUser sysUser:focusedUserList) {
            userVoList.add( copy(sysUser));
        }
        return userVoList;
    }

    private UserVo copy(SysUser sysUser) {
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(sysUser,userVo);
        userVo.setId( sysUser.getId().toString());
        return userVo;
    }

}
